package sybel.pan;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import sybel.DOMPanelInterface;

/**
 *
 * @author Stefano
 */
public class Include extends javax.swing.JPanel implements DOMPanelInterface{

    /** Creates new form Include */
    public Include(Node node) {
        father = node.getParentNode();
        include = (Element)node;
        initComponents();
        if(include != null)
            initPanel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextStepName = new javax.swing.JTextField();
        jTextStepID = new javax.swing.JTextField();
        jCBoptional = new javax.swing.JCheckBox();

        jLabel1.setText("StepName:");

        jLabel2.setText("StepID: ");

        jCBoptional.setText("Check if this inclusion is optional");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jCBoptional, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextStepID)
                            .addComponent(jTextStepName, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE))))
                .addContainerGap(97, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextStepName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextStepID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jCBoptional)
                .addContainerGap(52, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void initPanel(){
        setStepName(include.getAttribute("stepName"));
        setStepID(include.getAttribute("stepId"));
        if(include.hasAttribute("optional"))
            setOptional(true);
    }


    public String getStepName(){
        return jTextStepName.getText();
    }

    public String getStepID(){
        return jTextStepID.getText();
    }

    public boolean isOptional(){
        return jCBoptional.isSelected();
    }

    public void setStepName(String s){
        jTextStepName.setText(s);
    }

    public void setStepID(String s){
        jTextStepID.setText(s);
    }

    public void setOptional(boolean b){
        if(b)
            jCBoptional.setSelected(true);
        else
            jCBoptional.setSelected(false);
    }

    /**
     * Metodo che salva i dati riportati nel pannello creando e restituendo un nodo
     * "include" contenente appunto questi dati
     * @return il nodo include
     */
    public Node saveData(){
        Document doc = father.getOwnerDocument();
        include = doc.createElement("include");

        if(!getStepName().isEmpty())
            include.setAttribute("stepName",getStepName());
        if(!getStepID().isEmpty())
            include.setAttribute("stepId",getStepID());

        if(isOptional()){
            include.setAttribute("optional","true");
        }

        return (Node)include;
    }

    public Node AppendSavedData(){
        Node temp = include;
        Node newnode = saveData();
        father.replaceChild(newnode, temp);
        return father;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCBoptional;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextStepID;
    private javax.swing.JTextField jTextStepName;
    // End of variables declaration//GEN-END:variables
    private Node father;
    private Element include;
}
